package com.hd.trans.files.adapter;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;

import com.hd.trans.R;
import com.hd.trans.files.bean.Audio;
import com.hd.trans.files.holder.MediaFileViewHolder;
import com.hd.trans.files.sort.ISort;
import com.hd.trans.files.sort.MediaSortHelper;

import java.util.ArrayList;
import java.util.List;

public class MediaListAdapter extends BaseAdapter<Audio, MediaFileViewHolder> {

    private static final String CHECK_PAYLOAD = "checked";

    private List<Audio> mSelectedList = new ArrayList<>();

    public MediaListAdapter(List<Audio> selectedAudios) {
        super(new DiffUtil.ItemCallback<Audio>() {
            @Override
            public boolean areItemsTheSame(@NonNull Audio oldItem, @NonNull Audio newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull Audio oldItem, @NonNull Audio newItem) {
                return false;
            }
        });
        if (selectedAudios != null) {
            mSelectedList.clear();
            mSelectedList = selectedAudios;
        }
        addChildClickViewIds(R.id.media_cb_file);
    }

    @Override
    MediaFileViewHolder createViewHolder(View itemView) {
        return new MediaFileViewHolder(itemView);
    }

    @Override
    int getLayoutId() {
        return R.layout.layout_item_file;
    }

    @Override
    void bindViewHolder(MediaFileViewHolder holder, Audio audio) {
        holder.setChecked(mSelectedList.contains(audio));
        holder.setText(R.id.media_tv_file_name, audio.getDisplay_name());
        holder.setImageRes(R.id.media_iv_icon, audio.getImageRes());
        holder.showAudioExtraInfo(audio);
    }

    @Override
    protected void bindViewHolder(MediaFileViewHolder holder, Audio audio, @NonNull List<Object> payloads) {
//        holder.setChecked(mSelectedList.contains(audio));
    }

    public void updateSelectedAudios(List<Audio> audios) {
        mSelectedList = audios;
        notifyDataSetChanged();
    }

    /**
     * 根据排序规则进行排序
     *
     * @param method 排序规则
     */
    public void sort(ISort.SortMethod method) {
        List<Audio> sourceAudios = new ArrayList<>(getCurrentList());
        submitList(MediaSortHelper.getInstance().sortAudio(sourceAudios, method));
    }
}
